import RenderAuthorized from '@/components/Authorized';
import { getAuthority } from './authority';

let Authorized = RenderAuthorized(getAuthority()); // eslint-disable-line

const reloadToken = token => {
  window.localStorage.setItem('access-token', token);
};

// Reload the rights component
const reloadAuthorized = token => {
  reloadToken(token);
  Authorized = RenderAuthorized(getAuthority());
  if (!token) {
    window.localStorage.removeItem('login-p');
  }
};

const saveAuthorized = (restParams) => {
  if (restParams.autoLogin) {
    window.localStorage.setItem('login-p', JSON.stringify(restParams));
  } else {
    window.localStorage.removeItem('login-p');
  }
};

const getLoginP = () => window.localStorage.getItem('login-p');

const removeToken = () => window.localStorage.removeItem('access-token');

const getToken = () => window.localStorage.getItem('access-token');

export { reloadAuthorized, saveAuthorized, getToken, getLoginP };
export default Authorized;
